package com.orangelife.mobile.surrounding.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.curry.android.net.NetworkUtil;
import com.curry.android.util.ImageDownloader;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.surrounding.activity.HotGoodsDetailActivity;
import com.curry.android.adapter.FatherAdapter;

import java.io.Serializable;
import java.util.List;
import java.util.Map;


public class HotGoodsListViewAdapter<T> extends FatherAdapter<T> {

    private Context context;
    private List<T> mlist;

    private ImageDownloader image = ImageDownloader.getImageDownloader();

    @Override
    public BaseAdapter setList(List<T> list) {
        this.mlist=list;
        return HotGoodsListViewAdapter.this;
    }

    public HotGoodsListViewAdapter(Context context, List<T> list) {
        this.context = context;
        this.mlist = list;
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_lv_circumtesco_hotgoods, null);
            holder.ivPhoto = (ImageView) convertView.findViewById(R.id.ivPhoto);
            holder.tvGoodsName = (TextView) convertView.findViewById(R.id.tvGoodsName);
            holder.tvStandard = (TextView) convertView.findViewById(R.id.tvStandard);
            holder.tvPriceDiscount = (TextView) convertView.findViewById(R.id.tvPrice);
            holder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice2);
            holder.llHotGoods = (LinearLayout) convertView.findViewById(R.id.llHotGoods);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        /* listview填数据 */
        Map<String, Object> map = (Map<String, Object>) mlist.get(position);
        holder.tvGoodsName.setText(map.get("goodsName").toString());
        holder.tvStandard.setText(map.get("pdtDesc").toString());
        holder.tvPriceDiscount.setText(Constant.RMB_FLAG + map.get("price").toString());
        holder.tvPrice.setText(Constant.RMB_FLAG + map.get("mkPrice").toString());
        holder.tvPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        image.download(map.get("goodsLogo").toString(), holder.ivPhoto,
                ImageDownloader.ImageSize.IMAGE_SIZE_EXTRA_SMALL_RECTANGLE_JPG);
        holder.llHotGoods.setOnClickListener(new listener(map));

        return convertView;
    }

    class listener implements View.OnClickListener {

        private Map<String, Object> clikmap = null;

        public listener(Map<String, Object> map) {

            this.clikmap = map;
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.llHotGoods:
                    if (!NetworkUtil.isConnectedNet(context)) {
                        Toast.makeText(
                                context,
                                context.getResources().getString(
                                        R.string.net_disconnected),
                                Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("detail", (Serializable) clikmap);
                    intent.setClass(context, HotGoodsDetailActivity.class);
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                    break;

                default:
                    break;
            }

        }
    }

    class ViewHolder {
        /**
         * 照片
         */
        private ImageView ivPhoto;
        /**
         * 标题
         */
        private TextView tvGoodsName;
        /**
         * 重量
         */
        private TextView tvStandard;
        /**
         * 折扣价
         */
        private TextView tvPriceDiscount;
        /**
         * 现价
         */
        private TextView tvPrice;
        /**
         * 点击事件
         */
        private LinearLayout llHotGoods;


    }
}
